<?php

namespace App\Entity\MyData;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Repository\MyData\MyDataTemplateRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=MyDataTemplateRepository::class)
 */
class MyDataTemplate
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"mydata"})
     */
    private $id;

    /**
     * @Common(cnName="模板名称",listType="text",editType="text",width="")
     * @ORM\Column(type="string", length=255)
     * @Groups({"mydata"})
     */
    private $name;

    /**
     * @Common(cnName="模板名称",listType="text",editType="text")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"mydata"})
     */
    private $entity;

    /**
     * @ORM\OneToMany(targetEntity=MyDataKey::class, mappedBy="template", orphanRemoval=true)
     */
    private $myKeys;

    public function __construct()
    {
        $this->myKeys = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getEntity(): ?string
    {
        return $this->entity;
    }

    public function setEntity(?string $entity): self
    {
        $this->entity = $entity;

        return $this;
    }

    /**
     * @return Collection|MyDataKey[]
     */
    public function getMyKeys(): Collection
    {
        return $this->myKeys;
    }

    public function addMyKey(MyDataKey $myKey): self
    {
        if (!$this->myKeys->contains($myKey)) {
            $this->myKeys[] = $myKey;
            $myKey->setTemplate($this);
        }

        return $this;
    }

    public function removeMyKey(MyDataKey $myKey): self
    {
        if ($this->myKeys->contains($myKey)) {
            $this->myKeys->removeElement($myKey);
            // set the owning side to null (unless already changed)
            if ($myKey->getTemplate() === $this) {
                $myKey->setTemplate(null);
            }
        }

        return $this;
    }
}
